package com.knowledge.plus.比较器.内部比较器.示例;

import org.junit.Test;

import java.util.*;

/**
 * @author TPF
 * @since 2022/11/18
 */

public class PersonSortTest {

    @Test
    public void listSort() {
        List<Person> personList = new ArrayList<>();
        Person p1 = new Person("zhangsan", 12);
        Person p2 = new Person("lisi", 13);
        Person p3 = new Person("wangwu", 12);
        Person p4 = new Person("xiaoliuzi", 12);
        personList.add(p1);
        personList.add(p2);
        personList.add(p3);
        personList.add(p4);
        System.out.println("排序前：" + personList);
        Collections.sort(personList);
        System.out.println("排序后：" + personList);

    }

    @Test
    public void setSort() {
        Set<Person> treeSet = new TreeSet<>();
        Person p1 = new Person("zhangsan", 12);
        Person p2 = new Person("lisi", 13);
        Person p3 = new Person("wangwu", 12);
        Person p4 = new Person("xiaoliuzi", 12);
        treeSet.add(p1);
        treeSet.add(p2);
        treeSet.add(p3);
        treeSet.add(p4);
        System.out.println(treeSet);

        System.out.println("------------------------------------");

        TreeMap<Person, String> pdata = new TreeMap<Person, String>();
        pdata.put(new Person("张三", 30), "zhangsan");
        pdata.put(new Person("李四", 20), "lisi");
        pdata.put(new Person("王五", 10), "wangwu");
        pdata.put(new Person("小红", 5), "xiaohong");
        // 得到key的值的同时得到key所对应的值
        Set<Person> keys = pdata.keySet();
        for (Person key : keys) {
            System.out.println(key.getAge() + "-" + key.getName());
        }
    }

}
